<template>
	<view class="flex-column">
		<view class="baseImageView" style="flex-grow: 1;">
			<image style="width: 100%; height: 100%;" src="../../static/loginBage.png" mode=""></image>
		</view>
		
		<view class="baseView column-center" style="padding: 30rpx;">
			<view @click="backClick()" class="navBar column-center" style="margin-top: 10px;">
				<uni-icons type="left" size="20" color="rgb(153, 153, 153)" ></uni-icons>
			</view>
			<view class="column-center" style="">
				<view class="blockTextColor" style="font-size:70rpx; font-weight: bold;">
					欢迎使用
				</view>
				<view class="mainColor" style="font-size: 30rpx; margin-top: 20rpx;">
				    各种资源，天天更新
				</view>
				<view @click="loginBtnClick()" class="zsConter mainBgColor" style="border-radius: 8rpx; margin-top: 30rpx; width: 100%; height: 80rpx; color: white; font-size: 34rpx;">
					一键登录
				</view>
			</view>
		</view>
	</view>
</template> 

<script>

	export default {
		data() {
			return {
				statusBarHeight:0,
			}
		},
		onLoad() {
			const systemInfo = uni.getSystemInfoSync();
			const statusBarHeight = systemInfo.statusBarHeight;
		},
		methods: {
			backClick(){
				uni.navigateBack();
			},
			loginBtnClick(){
				
				
				uni.showLoading({
					title: '登录中'
				})
				this.$uniCloudApi.get_openId().then((res)=>{
					
					if(res != false){
						var openId = res;
						this.$uniCloudApi.query_user_data(res).then(res=>{
							console.log('查询结果=',res);
							if(res.existence == true){
								this.saveLocalUserInfo(res.data);
								
							} else {
								//没有
								var model ={};
								model.openId = openId;
								model.memberType = '0';
								model.integral = '30'; //默认积分
								this.$uniCloudApi.add_user_data(model).then((res2)=>{
									
									console.log('注册成功=',res2);
									model.id = res2.result.data;
									this.saveLocalUserInfo(model);
								}).catch(errorStr=>{
							uni.showToast({
								title:errorStr
							})
						})
							}
						}).catch(errorStr=>{
							uni.showToast({
								title:errorStr
							})
						})
					}
				}).catch(error=>{
					uni.showToast({
						title:error
					})
					// uni.hideLoading();
				})
			},
			saveLocalUserInfo(userInfo){
				var that = this;
				uni.setStorage({
					key: 'userInfo',
						data:userInfo,
					success(lres) {
						that.$setUserInfo(userInfo);
						console.log('存本地成功=',lres)
						uni.showToast({
							title:'登录成功'
						})
						setTimeout(function () {
							uni.hideLoading();
							that.backClick();
						}, 1000);
					},
					fail(failres) {
						console.log('存本地失败=',failres)
					}
					
				})
			},
			backClick(){
				uni.navigateBack();
			}
		}
	}
</script>

<style >
	.page {
	  height: 100vh;
	}
	.baseView{
		position: absolute;
		left: 0;
		right: 0;
		height: 100vh;
	}
	.baseImageView{
		width: 100%;
		height: 100vh;
	}
	.navBar{
		position: absolute;
		left: 0rpx;
		top: 0rpx;
		width: 100%;
		height: calc({{statusBarHeight}}px + 44px); /* 假设标题栏高度为44px */
		padding: 40rpx 40rpx;
	}
	


</style>
